﻿

namespace EAI.ServiceHost
{
    using System;
    using System.Diagnostics;
    using System.Linq;

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Description;

    using EAI.Logging;


    public class EAIMessageInspector : IDispatchMessageInspector, IClientMessageInspector
    {
        private Message TraceMessage(MessageBuffer buffer)
        {
            Message msg = buffer.CreateMessage();

            Log4NetHelper.GetLogger().Debug(msg);

            return msg;
        }

        public object AfterReceiveRequest(ref Message request,
            IClientChannel channel,
            InstanceContext instanceContext)
        {
            request = TraceMessage(request.CreateBufferedCopy(int.MaxValue));
            return null;
        }
        public void BeforeSendReply(ref Message reply, object
            correlationState)
        {
            reply = TraceMessage(reply.CreateBufferedCopy(int.MaxValue));
        }

        public void AfterReceiveReply(ref Message reply, object
            correlationState)
        {
            reply = TraceMessage(reply.CreateBufferedCopy(int.MaxValue));
        }
        public object BeforeSendRequest(ref Message request,
            IClientChannel channel)
        {
            request = TraceMessage(request.CreateBufferedCopy(int.MaxValue));
            return null;
        }
    }
}
